<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">

    <script src="/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
    <script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script src="/lib/jq-module/jquery.particleground.min.js" charset="utf-8"></script>

</head>

<body>
    <div class="layuimini-container">
        <div class="layuimini-main">

            <fieldset class="table-search-fieldset">
                <legend>搜索信息</legend>
                <div style="margin: 10px 10px 10px 10px">
                    <form class="layui-form layui-form-pane" action="">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">老人姓名</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="oldman_name" autocomplete="off" class="layui-input">
                                </div>
                            </div>

                            <div class="layui-inline">
                                <label class="layui-form-label">地址</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="oldman_address" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">性别</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="oldman_sex" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            </div>
                        </div>
                    </form>
                </div>
            </fieldset>

            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                    <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
                    <button class="layui-btn layui-btn-sm layui-btn-normal data-delete-btn" lay-event="export"> 导出 </button>
                    <button class="layui-btn layui-btn-sm layui-btn-warm data-delete-btn" lay-event="import"> 导入 </button>
                    <button class="layui-btn layui-btn-sm layui-btn-normal data-delete-btn" lay-event="bar"> 男女比例柱状图 </button>
                    <button class="layui-btn layui-btn-sm layui-btn-warm data-delete-btn" lay-event="pie"> 男女比例饼状图 </button>
                </div>
            </script>

            <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

            <script type="text/html" id="currentTableBar">
                <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
                
            </script>
            <script id="picTpl">
                <img src="{{d.pic}}" name="pic" style="width:100%;height:100%" />
            </script>
        </div>
    </div>
    <script>
        layui.use(['form', 'table'], function () {
            var $ = layui.jquery,
                form = layui.form,
                table = layui.table;
            -
                table.render({
                    elem: '#currentTableId',
                    url: 'http://localhost:8080/people/peoplelist',
                    toolbar: '#toolbarDemo',
                    defaultToolbar: ['filter', 'exports', 'print', {
                        title: '提示',
                        layEvent: 'LAYTABLE_TIPS',
                        icon: 'layui-icon-tips'
                    }],
                    cols: [[
                        { type: "checkbox", width: 50 },
                        { field: 'oldman_id', width: 80, title: 'ID', sort: true },
                        { field: 'oldman_name', width: 90, title: '用户名' },
                        { field: 'oldman_sex', width: 80, title: '性别' },
                        { field: 'oldman_age', width: 80, title: '年龄', sort: true },
                        { field: 'oldman_address', width: 150, title: '住址', sort: true },
                        { field: 'oldman_birthday', width: 150, title: '生日' },
                        { field: 'oldman_phone', title: '联系方式', minWidth: 150 },
                        { field: 'oldman_pic', width: 150, title: '头像', templet: '#picTpl' },

                        { title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center" }
                    ]],
                    limits: [10, 15, 20, 25, 50, 100],
                    limit: 10,
                    page: true,
                    skin: 'line'
                });

            // 监听搜索操作
            form.on('submit(data-search-btn)', function (data) {
                var result = JSON.stringify(data.field);
                layer.alert(result, {
                    title: '最终的搜索信息'
                });

                //执行搜索重载
                table.reload('currentTableId', {
                    page: {
                        curr: 1
                    }
                    , where: {
                        // searchParams: result
                        oldman_name: data.field.oldman_name,
                        oldman_address: data.field.oldman_address,
                        oldman_sex: data.field.oldman_sex,
                    }
                }, 'data');

                return false;
            });

            /**
             * toolbar监听事件
             */
            table.on('toolbar(currentTableFilter)', function (obj) {
                if (obj.event === 'add') {  // 监听添加操作
                    var index = layer.open({
                        title: '添加用户',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '/page/older/peopleadd.html',


                        end: function (index) {
                            // 列表重新加载
                            table.reload('currentTableId');
                        }
                    });

                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                } else if (obj.event === 'delete') { // 监听删除操作
                    var checkStatus = table.checkStatus('currentTableId')
                        , data = checkStatus.data;
                    var str = "";
                    for (var i in data) {
                        str += data[i].oldman_id + "-";
                    }
                    var data2 = { "oldman_id": str };
                    //获取选中数量

                    var selectCount = checkStatus.data.length;
                    if (selectCount == 0) {
                        layer.msg('至少选一个吧', function () { });
                        return false;
                    }
                    layer.confirm('真的要删除选中的项吗？', function (index) {
                        layer.close(index);
                    })
                    $.ajax({
                        type: "get",
                        data: data2,
                        url: "http://localhost:8080/people/deleteAll",
                        success: function (data) {
                            if (data.code == 0) {
                                $('#reload').click();
                            }
                            layer.close(index);
                            layer.msg('删除成功！');
                            table.reload('currentTableId', {});
                        }, erro: function (code) {
                            layer.msg('操作失败！');
                        }
                    })

                } else if (obj.event === 'bar') {
                    layer.open({
                        title: '男女比例柱状图',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['80%', '100%'],
                        content: 'bar.html',
                        end: function (index) {

                        }
                    })
                } else if (obj.event === 'pie') {
                    layer.open({
                        title: '男女比例饼状图',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['80%', '100%'],
                        content: 'pie.html',
                        end: function (index) {

                        }
                    })
                }

            });

            //监听表格复选框选择
            table.on('checkbox(currentTableFilter)', function (obj) {
                console.log(obj)
            });
            
            table.on('tool(currentTableFilter)', function (obj) {
                var data = obj.data;
                if (obj.event === 'edit') {
                    localStorage.setItem('people', JSON.stringify(data));
                    var index = layer.open({
                        title: '编辑用户',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '/page/older/peopleedit.html',
                        end: function (index) {
                            // 列表重新加载
                            table.reload('currentTableId');
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                } else if (obj.event === 'delete') {
                    layer.confirm('真的删除行么', function (index) {
                        console.log(data.oldman_id)
                        $.ajax({
                            url: 'http://localhost:8080/people/delete',
                            type:'post',
                            datatype:'json',
                            data:{oldman_id:data.oldman_id},
                            success: function (data) {
                                if (data.code == 0) {
                                    alert('删除成功')
                                    obj.del();
                                }
                            }
                        })

                        layer.close(index);
                    });
                }
            });

        });
    </script>
</body>

</html>